package com.netmi.myjectpack;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;

import com.netmi.myjectpack.databinding.ActivityMainBinding;
import com.netmi.myjectpack.fragment.DataBindFragment;
import com.netmi.myjectpack.fragment.LiveDataDetailsFragment;
import com.netmi.myjectpack.fragment.LiveDataFragment;
import com.netmi.myjectpack.fragment.CustomLiveDataFragment;
import com.netmi.myjectpack.paging.Paging3MainActivity;
import com.netmi.myjectpack.startup.RealmFragment;

public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {

    private ActivityMainBinding binding;

    //利用单选按钮控制fragment的显示与隐藏
    private CompoundButton selectView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);


        binding.setCheckListener(this);
        binding.rbLivedata.setTag(LiveDataFragment.TAG);
        binding.rbDatabind1.setTag(DataBindFragment.TAG);
        binding.rbLivedataDetailse.setTag(LiveDataDetailsFragment.TAG);
        binding.rbLivedataStock.setTag(CustomLiveDataFragment.TAG);
        binding.rbStartup.setTag(RealmFragment.TAG);
        binding.executePendingBindings();

        binding.rbLivedata.setChecked(true);


    }

    public void doPagingClick(View view){
        startActivity(new Intent(MainActivity.this, Paging3MainActivity.class));
    }


    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
        Log.d("tgw", "onCheckedChanged: " + isChecked);

        if (isChecked) {
            compoundButton.setBackgroundColor(R.color.colorPrimary);
            showFragment(compoundButton);
        } else {
            compoundButton.setBackgroundColor(Color.parseColor("#ffffff"));
            hideFragment(compoundButton);
        }
    }


    private void showFragment(CompoundButton curView) {
        if (selectView != null) {
            selectView.setChecked(false);
        }
        selectView = curView;
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        String tag = (String) curView.getTag();


        Fragment fragment = fragmentManager.findFragmentByTag(tag);

        if (fragment == null) {
//            val factory: FragmentFactory = fragmentManager.fragmentFactory
//            fragment = factory.instantiate(classLoader, tag)
            fragment = Fragment.instantiate(this, tag);
            fragmentTransaction.add(R.id.fl_content, fragment, tag);
            fragmentTransaction.commitAllowingStateLoss();
        } else {
            boolean isHide = fragment.isHidden();
            if (isHide) {
                fragmentTransaction.show(fragment);
                fragmentTransaction.commitAllowingStateLoss();
                fragment.onResume(); //切换Fragment，实时刷新数据
            }
        }
    }


    private void hideFragment(CompoundButton lastView) {
        if (lastView != null) {
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            String tag = (String) lastView.getTag();
            if (fragmentManager.findFragmentByTag(tag) != null) {
                boolean isHide = fragmentManager.findFragmentByTag(tag).isHidden();
                if (!isHide) {
                    fragmentTransaction.hide(fragmentManager.findFragmentByTag(tag));
                    fragmentTransaction.commitAllowingStateLoss();
                }
            }
        }
    }
}
